package hnx.ca.xml.verifier;

/*
 *============================================================================
 * Signerlet, the applet for signing documents from web with token
 * Copyright (c) 2011 VNPT Epay Joint Stock Company
 *============================================================================
 */
import java.io.*;

/**
 * Class for file operations
 * @author Dinh Quoc Dat
 * @version 2011.03.0816
 */
public class file {
  
  /**
   * Class level constructor
   */
  static {
    //
  }
  
  /**
   * Default constructor
   */
  public file() {
    //
  }
  
  /**
   * Append text to the end of a file
   * @param file_path The path to file being appended
   * @param text The text to append to file
   * @return 'true' if succeeded, 'false' otherwise
   */
  public static boolean append_text(String file_path,String text) {
    boolean appending = true;
    
    try {
      FileOutputStream writer = new FileOutputStream(file_path,appending);
      writer.write(text.getBytes());
      writer.close();
      return true;
    }
    catch (Exception exception) {
      return false;
    }
  }//append text
  
  /**
   * Save a text content to file
   * @param file_path The full path to file
   * @param text The content to save to file
   */
  public static void save_to_file(String file_path,String text) {
    try {
      FileOutputStream output = new FileOutputStream(file_path);
      output.write(text.getBytes());
      output.close();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }
  
  /**
   * Save byte array to text file
   * @param file_path The full file path
   * @param bytes The bytes to save
   */
  public static void save_to_file(String file_path,byte[] bytes) {
    save_to_file(file_path,new String(bytes));
  }
  
  /**
   * Get file size of a file specified by file_path
   * @param file_path The path to file, eg c:/.../some_file.txt
   * @return The length (file size) of file
   */
  public static long get_file_size(String file_path) {
    File file = new File(file_path);
    return file.length();
  }
  
  /**
   * Read all bytes in a file to byte array
   * @param file_path The path to local file
   * @return All the bytes in file, 'null' if IO error
   */
  public static byte[] read_all_bytes(String file_path) {
    try {
      FileInputStream reader = new FileInputStream(file_path);
      int file_size = (int)get_file_size(file_path);
      byte[] bytes = new byte[file_size];
      reader.read(bytes);
      reader.close();
      return bytes;
    }
    catch (Exception exception) {
      
      exception.printStackTrace();
      return null;
    }
  }//read all bytes  
  
  /**
   * Get the extension of a file
   * @param file_path The full file path or file name
   * @return The extension of file, eg. 'jpg', 'zip', etc.
   */
  public static String get_extension(String file_path) {
    if (file_path==null)
      return null;
    file_path = file_path.trim();
    
    //find position of the dot '.'
    int dot_pos = file_path.lastIndexOf(".");
    if (dot_pos==-1)
      return "";
    
    //get extension
    String extension = file_path.substring(dot_pos);
    if (extension.length()<=1)
      return "";
    extension = extension.substring(1);
    
    return extension.toLowerCase();
  }
  
  /**
   * Check if a file is office 2007+ file, ie. docx,xlsx,pptx
   * @param file_path The path to file
   * @return 'true' if is office 2007+ file
   */
  public static boolean is_officex_file(String file_path) {
    String extension = file.get_extension(file_path);
    return (extension.equals("docx") || extension.equals("xlsx") ||
      extension.equals("pptx"));
  }
  
  /**
   * Get the file name part in a full file path
   * @param full_path The full file path to a file
   * @return The file name extracted
   */
  public static String get_file_name(String full_path) { 
    full_path = full_path.replace("\\","/"); 
    int slash_pos = full_path.lastIndexOf("/"); 
    if (slash_pos==-1)
      return "";
    
    String file_name = full_path.substring(slash_pos);
    if (file_name.length()<=1)
      return "";
    
    return file_name.substring(1).toLowerCase();
  }
  
  /**
   * Get the path portion from a full path, meaning exlude file name
   * @param full_path The full path to a file
   * @return The path only portion
   */
  public static String get_path(String full_path) {
    full_path = full_path.replace("\\","/");
    int slash_pos = full_path.lastIndexOf("/");
    if (slash_pos==-1)
      return "";
    
    String path = full_path.substring(0,slash_pos);
    return path.toLowerCase();
  }
}